package org.qengine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.qengine.dao.PaperInfoDao;
import org.qengine.entity.PaperInfoEntity;
import org.qengine.entity.UserAnswersEntity;
import org.qengine.service.PaperInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.qengine.service.UserAnswersService;

import java.util.ArrayList;
import java.util.List;

@Service
public class PaperInfoServiceImpl extends ServiceImpl<PaperInfoDao, PaperInfoEntity>
        implements PaperInfoService {

    @Autowired
    private PaperInfoDao paperInfoDao;

    @Autowired
    private UserAnswersService userAnswersService;


    private UserAnswersEntity userAnswersEntity=new UserAnswersEntity();

    @Override
    public List<Long> getPaperIDsByStuID(Long stuID) {
        // 从 paperInfo 表中查询指定 stuID 的所有试卷 id
        LambdaQueryWrapper<PaperInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperInfoEntity::getStuID, stuID);
        List<PaperInfoEntity> paperInfos = paperInfoDao.selectList(queryWrapper);

        // 将查询到的试卷 id 组成一个列表
        List<Long> paperIDs = new ArrayList<>();
        for (PaperInfoEntity paperInfo : paperInfos) {
            paperIDs.add(paperInfo.getPaperID());
        }
        return paperIDs;
    }

    @Override
    public PaperInfoEntity getPaperInfoById(Long paperID) {
        return paperInfoDao.getPaperInfoById(paperID);
    }

    @Override
    public int updatePaperInfo(PaperInfoEntity paperInfoEntity) {
        // 检查是否存在相同的 paperID 记录
        QueryWrapper<PaperInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stuID", paperInfoEntity.getStuID());

        PaperInfoEntity existingStuInfo = paperInfoDao.selectOne(queryWrapper);
        if (existingStuInfo != null) {
            // 如果存在，则更新记录
            return paperInfoDao.update(paperInfoEntity, queryWrapper);
        } else {
            // 如果不存在，则插入新记录
            return paperInfoDao.insert(paperInfoEntity);
        }
    }

    @Override
    public int deletePaperInfo(Long paperID) {
        return paperInfoDao.deletePaperInfo(paperID);
    }

    @Override
    public List<PaperInfoEntity> getPaperInfo() {
        return paperInfoDao.getPaperInfo();
    }

    @Override
    public PaperInfoEntity updateAnsStatus(UserAnswersEntity answer) {
        String list = userAnswersService.stateList(answer);//使用UserAnswersService里的stateList函数
        //根据paperId获取paperInfoEntity
        Integer paperId = answer.getPaperId();
        PaperInfoEntity paperInfoEntity = paperInfoDao.getPaperInfoById(Long.valueOf(paperId));
        //更新ansStatus
        paperInfoEntity.setAnsStatus(list);
        return paperInfoEntity;
    }

    @Override
    public PaperInfoEntity updateScore( UserAnswersEntity answer){
        String list = userAnswersService.stateList(answer);
        char[] chars = list.toCharArray();
        Integer cnt = 0;
        Integer score =0;
        for (char c : chars) {
            if (c == '1') {
                cnt++;
            }
        }
        score = 2*cnt; //每题2分
        Integer paperId = answer.getPaperId();
        PaperInfoEntity paperInfoEntity = paperInfoDao.getPaperInfoById(Long.valueOf(paperId));
        paperInfoEntity.setScore(score);
        return paperInfoEntity;
    }



    // 根据学生ID和试卷ID获取学生的答卷信息
    public UserAnswersEntity getStuAnswersByStuIDAndPaperID(Long stuID, Long paperID) {
        // 构造查询条件
        LambdaQueryWrapper<UserAnswersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAnswersEntity::getUserId, stuID)
                .eq(UserAnswersEntity::getPaperId, paperID);

        // 查询并返回结果
        return userAnswersService.getOne(queryWrapper);
    }

    // 根据学生ID和试卷ID更新学生的答卷信息
    public boolean updateStuAnswersByStuIDAndPaperID(UserAnswersEntity userAnswersEntity) {
        // 构造查询条件
        LambdaQueryWrapper<UserAnswersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAnswersEntity::getUserId, userAnswersEntity.getUserId())
                .eq(UserAnswersEntity::getPaperId, userAnswersEntity.getPaperId());

        // 更新并返回结果
        return userAnswersService.update(userAnswersEntity, queryWrapper);
    }

    // 根据学生ID和试卷ID删除学生的答卷信息
    public boolean deleteStuAnswersByStuIDAndPaperID(Long stuID, Long paperID) {
        // 构造查询条件
        LambdaQueryWrapper<UserAnswersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAnswersEntity::getUserId, stuID)
                .eq(UserAnswersEntity::getPaperId, paperID);

        // 删除并返回结果
        return userAnswersService.remove(queryWrapper);
    }

}
